import osimport base64import plotly.graph_objects as go# Directory containing imagesimage_folder ='cps_plots_folder'image_files = [f for f in os.listdir(image_folder) if f.endswith('.png')]# Function to convert image to base64def image_to_base64(image_path):withopen(image_path, "rb") as image_file:return base64.b64encode(image_file.read()).decode('utf-8')# Convert all images to base64 stringsimage_sources = [f"data:image/png;base64,{image_to_base64(os.path.join(image_folder, img))}"for img in image_files]# Define figurefig = go.Figure()# Add traces for each image and use visible=False initially except the firstfor idx, image_src inenumerate(image_sources): fig.add_trace( go.Image( source=image_src, visible=(idx ==0) # Only the first image is visible by default ) )# Create dropdown options to toggle visibility of imagesdropdown_buttons = [dict( label=image_files[idx], method="update", args=[ {"visible": [i == idx for i inrange(len(image_sources))]}, # Show only the selected image {"title": f"{image_files[idx]}"} ] )for idx inrange(len(image_sources))]# Update layout with dropdownfig.update_layout( updatemenus=[dict( buttons=dropdown_buttons, direction="down", showactive=True, x=0.01, y=1.15, xanchor="left", yanchor="top" ) ], title="Interactive Plot Viewer with Dropdown", title_x=0.5)# Display the figurefig.show()